#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from wsgi import create_app
from app.models.work_order import WorkOrder
from app.models.user import User

def assign_work_order():
    """将工单4分配给维护人员"""
    app = create_app()
    
    with app.app_context():
        # 获取工单4
        work_order = WorkOrder.query.get(4)
        if not work_order:
            print("工单4不存在")
            return
        
        # 获取一个维护人员（用户ID=5，18109984459）
        worker = User.query.get(5)
        if not worker:
            print("维护人员不存在")
            return
        
        print(f"将工单4分配给维护人员: {worker.username} ({worker.name})")
        
        # 分配工单
        work_order.assignee_id = worker.id
        work_order.status = 'in_progress'  # 更新状态为处理中
        
        try:
            from app.models import db
            db.session.commit()
            print("工单分配成功！")
            
            # 验证分配结果
            updated_order = WorkOrder.query.get(4)
            print(f"验证: 工单4现在分配给用户ID {updated_order.assignee_id}")
            
        except Exception as e:
            from app.models import db
            db.session.rollback()
            print(f"分配失败: {e}")

if __name__ == '__main__':
    assign_work_order()